package com.yonyou.component;

import com.yonyou.mapper.UserMapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;


@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {


    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // 并不是直接注册动态代理， 而是注册FactoryBean
        //1、扫描 继承 ClasspathBeanDefinitionScanner 重写 isCandidateComponent(这个方法接口不允许扫描)
        // ClasspathMapperScanner scanner = new ClasspathMapperScanner();
        //Set<BeanDefinitionHold>  类型 UserMapper.class mybatis整合的时候所有的 mapper beanDefinition 类型是 FactoryBean
        // 扫描所有mapper接口  所有mapper接口类
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        // 偷天换日
        beanDefinition.setBeanClass(MyFactoryBean.class);

        beanDefinition.getPropertyValues().addPropertyValue("mapperInterFace", UserMapper.class);

        registry.registerBeanDefinition("userMapper2",beanDefinition);

        // 扫描所有mapper接口

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
